package com.andnux.android.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.andnux.android.adapter.MyOrderAdapter;
import com.andnux.android.data.Orders;
import com.andnux.android.data.PageData;
import com.andnux.android.databinding.ActivityMyOrderBinding;
import com.andnux.android.network.DesignCloudApi;
import com.andnux.android.network.NotNullConsumer;
import com.andnux.android.network.Result;
import com.andnux.android.network.RetrofitClient;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MyOrderActivity extends BaseActivity<ActivityMyOrderBinding> implements TabLayout.OnTabSelectedListener {

    private static final String TAG = "MyOrderActivity";
    private DesignCloudApi api;
    private int current = 0;
    private static final int size = 10;
    private final List<Orders> list = new ArrayList<>();
    private Integer status = null;
    private MyOrderAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = RetrofitClient.getInstance().getServerApi();
        status = getIntent().getIntExtra("status", -1);
        if (status < 0) {
            status = null;
        }
        binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        adapter = new MyOrderAdapter(this);
        binding.content.setAdapter(adapter);
        binding.smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                current++;
                loadData();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                current = 0;
                list.clear();
                adapter.setNewData(list);
                loadData();
            }
        });

        binding.tab.addOnTabSelectedListener((TabLayout.OnTabSelectedListener) this);
        if (status == null) {
            binding.tab.selectTab(binding.tab.getTabAt(0));
        } else {
            binding.tab.selectTab(binding.tab.getTabAt(status + 1));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.smart.autoRefresh();
    }

    private void loadData() {
        addDisposable(api.orderPage(current, size, status)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NotNullConsumer<Result<PageData<List<Orders>>>>() {
                    @Override
                    public void onSuccess(Result<PageData<List<Orders>>> result) {
                        List<Orders> data = result.getData().getList();
                        list.addAll(data);
                        adapter.setNewData(list);
                        if (data.size() < size) {
                            binding.smart.finishLoadMoreWithNoMoreData();
                        } else {
                            binding.smart.finishLoadMore();
                        }
                        binding.smart.finishRefresh();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        binding.smart.finishLoadMoreWithNoMoreData();
                        binding.smart.finishRefresh();
                        if (throwable != null) {
                            Toast.makeText(MyOrderActivity.this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }));
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int position = tab.getPosition() - 1;
        if (position < 0) {
            status = null;
        } else {
            status = position;
        }
        list.clear();
        adapter.setNewData(list);
        Log.d(TAG, "onTabSelected() called with: status = [" + status + "]");
        binding.smart.autoRefresh();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}